package com.example.dllo.exe.remen;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.exe.bean.ReMenBean;
import com.example.dllo.exe.R;
import com.example.dllo.exe.fengzhuang.NetTool;



/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class ReMenRvAdapter extends RecyclerView.Adapter<ReMenRvAdapter.MyViewHolder>  {
    private Context context;
    private ReMenBean reMenBean;


    ImageView imageremenonclick;
    public void setReMenBean(ReMenBean reMenBean) {
        this.reMenBean = reMenBean;
        notifyDataSetChanged();
    }

    public ReMenRvAdapter(Context context) {
        this.context = context;

    }





    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int position,ReMenBean reMenBean);

    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    public void setmOnItemClickListener(OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public ReMenRvAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.remenfragmentrvlayout, parent, false);
//        remenonclick = (RelativeLayout) view.findViewById(remenrv_onclick);
        imageremenonclick= (ImageView) view.findViewById(R.id.Image_ReMen);
        MyViewHolder viewHolder = new MyViewHolder(view);

        return viewHolder;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(final ReMenRvAdapter.MyViewHolder holder, int position) {
        try {
            holder.favoritestext.setText(reMenBean.getData().getItems().get(position).getData().getFavorites_count() + "");
            holder.nametext.setText(reMenBean.getData().getItems().get(position).getData().getName());
            holder.pricetext.setText(reMenBean.getData().getItems().get(position).getData().getPrice());
            NetTool netTool = new NetTool();
            netTool.getImage(reMenBean.getData().getItems().get(position).getData().getCover_image_url(), holder.imageImgurl);
           imageremenonclick.setTag(position);
        } catch (NullPointerException e) {
            Log.d("ReMenRvAdapter", "ReMenAdapter" + position);
        }

        holder.imageImgurl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = holder.getLayoutPosition();
//                Toast.makeText(context, "pos:" + pos, Toast.LENGTH_SHORT).show();
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(pos,reMenBean);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return reMenBean != null ? reMenBean.getData().getItems().size() : 0;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nametext;
        TextView pricetext;
        TextView favoritestext;
        ImageView imageImgurl;

        public MyViewHolder(View itemView) {
            super(itemView);
            nametext = (TextView) itemView.findViewById(R.id.Name_ReMen);
            pricetext = (TextView) itemView.findViewById(R.id.Price_ReMen);
            imageImgurl = (ImageView) itemView.findViewById(R.id.Image_ReMen);
            favoritestext = (TextView) itemView.findViewById(R.id.Favorites_ReMen);

        }


    }
}
